/**
 * Created by lixin on 2019-11-15
 *  pages 页面快速生成脚本
 *  npm run tep '文件名‘
 */
const fs = require('fs');

/* 属性返回一个数组，其中包含当启动 Node.js 进程时传入的命令行参数 */
const dirName = process.argv[2];

if (!dirName) {
  console.log('🤣🤣🤣🤣🤣🤣🤣🤣🤣🤣🤣🤣🤣🤣🤣🤣🤣🤣');
  console.log('文件名不能为空');
  console.log('用法：npm run tep 文件名');
  console.log('-----------------------------------------');
  process.exit(0);
}

const capPirName = dirName.substring(0, 1).toUpperCase() + dirName.substring(1);

/* 页面模板构建 */
const indexTep = `
import Taro, { Component } from '@tarojs/taro'
import { View } from '@tarojs/components'
import { connect } from '@tarojs/redux'
import './${dirName}.less'

@connect(({ ${dirName} }) => ({
     ...${dirName},
 }))

class ${capPirName} extends Component{
  config = {
      navigationBarTitleText: '页面标题'
  }
  constructor(props) {
      super(props)
      this.state = {}
  }
  
  componentWillMount () { }
  
  componentDidMount () { }
  
  componentWillUnmount () { }
  
  componentDidShow () { }
  
  componentDidHide () { }
  
  render() {
      return (
      <View className='fx-${dirName}-wrap'>
          页面内容
      </View>
      )
  }
}
export default ${capPirName}`;

/* model 模板 */
const modelTep = `
export default {
    namespace: '${dirName}',
    state: {
    },
    
    effects: {},
    
    reducers: {}

}`;

/* less 模板 */
const lessTep = `
.fx-${dirName}-wrap{
  width: 100%;
  min-height: 100Vh;
  background: #f7f7f7;
}`;

try {
  /* mkdir 创建组件文件夹*/
  fs.mkdirSync(`./src/pages/${dirName}`);
  /* 进入到新建文件夹 */
  process.chdir(`./src/pages/${dirName}`);
  /* 创建文件，页面模板文件和less文件*/
  fs.writeFileSync(`${dirName}.jsx`, indexTep);
  fs.writeFileSync(`${dirName}.less`, lessTep);
  /* 进入到models文件夹中创建model*/
  process.chdir('../../models');
  fs.writeFileSync(`${dirName}.js`, modelTep);
} catch (err) {
  console.log('--------------错误信息----------------------');
  console.error(`chdir:${err}`);
  console.log('----------------end-------------------');
}
/* 退出状态 默认值0 */
process.exit(0);


